package com.wake.mycomposebook.ui.home.layouts.advlist

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.FavoriteBorder
import androidx.compose.material3.IconToggleButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.wake.data.DemoDataProvider
import com.wake.data.model.Item
import com.wake.theme.modifier.components.Material3Card

/**
 *@author created by WA03675
 *@date  2022-04-20 17:15
 *@des
 */

/**
 * 如何知道移动了多长
 * 回弹
 * 滑动
 */
@Composable
fun MySwiper() {
    val datas = DemoDataProvider.itemList.take(5)
    var selectIndex = remember {
        mutableStateOf(0)
    }

    Column(
        Modifier
            .fillMaxWidth()
            .padding(20.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        MySwiper1(datas, selectIndex)
        Spacer(modifier = Modifier.height(40.dp))
        MySwiper2(datas, selectIndex)
        Spacer(modifier = Modifier.height(40.dp))
        MySwiper3(datas, selectIndex)
    }
}

@Composable
fun MySwiper1(datas: List<Item>, selectIndex: MutableState<Int>) {
    LazyRow {
        items(datas, key = { item -> return@items item.id }) {
            Image(
                painter = painterResource(id = it.imageId),
                contentDescription = null,
                Modifier
                    .padding(30.dp, 0.dp)
                    .clip(RoundedCornerShape(15.dp))
            )
        }
    }

    Row(
        Modifier
            .padding(16.dp)
            .fillMaxWidth(),
        horizontalArrangement = Arrangement.Center
    ) {
        datas.forEachIndexed { index, _ ->
            Spacer(
                modifier = Modifier
                    .size(30.dp)
                    .clip(CircleShape)
                    .background(if (index == selectIndex.value) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onBackground)
                    .clickable { selectIndex.value = index }
            )
        }
    }

}

/**
 * 第一个如何在中间?
 * 滑动的时候又会到一边去?
 *
 * @param datas
 * @param selectIndex
 */
@Composable
fun MySwiper2(datas: List<Item>, selectIndex: MutableState<Int>) {
    LazyRow {
        items(
            datas,
            key = { item -> return@items item.id },
            itemContent = {
                Image(
                    painter = painterResource(id = it.imageId),
                    contentDescription = null,
                    Modifier
                        .padding(30.dp, 0.dp)
                        .size(100.dp)
                        .clip(CircleShape),
                    contentScale = ContentScale.Crop
                )
            }
        )
    }

    Spacer(
        modifier = Modifier
            .height(20.dp)
            .fillMaxWidth()
    )

    Row(horizontalArrangement = Arrangement.Center) {
        datas.forEachIndexed { index, _ ->
            IconToggleButton(checked = true, onCheckedChange = { selectIndex.value = index }, content = {
                Image(
                    imageVector = if (selectIndex.value == index) Icons.Default.Favorite else Icons.Default.FavoriteBorder,
                    contentDescription = null,
                    modifier =
                    Modifier
                        .padding(15.dp, 5.dp)

                )
            })
        }
    }
}


@Composable
fun MySwiper3(datas: List<Item>, selectIndex: MutableState<Int>) {
    LazyRow() {
        items(datas,
            key = { item -> return@items item.id },
            itemContent = {
                Material3Card(
                    Modifier
                        .padding(15.dp)
                        .width(150.dp)
                        .height(300.dp)
                        .clip(RoundedCornerShape(15.dp)),
                    contentColor = MaterialTheme.colorScheme.primary,
                    elevation = 10.dp,
                ) {
                }
            }
        )
    }

    datas.forEachIndexed { index, _ ->
        Spacer(
            modifier = Modifier
                .size(30.dp)
                .border(1.dp, MaterialTheme.colorScheme.onBackground, CircleShape)
                .clip(CircleShape)
                .background(if (selectIndex.value == index) Color.Red else Color.Yellow)
                .clickable { selectIndex.value = index },
        )
    }
}
